//
//  HXHomeViewController.m
//  爱鲜蜂
//
//  Created by 建国 on 16/3/27.
//  Copyright © 2016年 建国. All rights reserved.
//
#import "HXExpandViewController.h"
#import "HXButtonView.h"
#import "HXHotReusableView.h"
#import "HXHotCollectionCell.h"
#import "HXButton.h"
#import "XMGInfiniteScrollView.h"
#import "HXrecommendCell.h"
#import "HXActiveView.h"
#import "HXQRCodeVC.h"
#import "HXTopTitleView.h"
#import "HXSerachVC.h"

#import "HXHomeViewController.h"

@interface HXHomeViewController()<UICollectionViewDataSource
,UICollectionViewDelegate>


@property (nonatomic ,weak) UICollectionView* hotView ;

@end

@implementation HXHomeViewController
static NSString *hotHeaderID = @"hotHeaderID";
static NSString *hotID = @"hotcellID";
static NSString *const ID = @"goods" ;

- (void)viewDidLoad {
    [super viewDidLoad];
    //创建collectionView
    [self setUpdateViewController];

}

//设置控制器
-(void)setUpdateViewController{

    //创建collectionView
    self.automaticallyAdjustsScrollViewInsets = NO ;
    self.tableView.contentInset = UIEdgeInsetsMake(260, 0, 0, 0);
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone ;
    self.tableView.showsVerticalScrollIndicator = NO ;
    [self setupActiveView];
    [self.tableView registerNib:[UINib nibWithNibName:@"HXrecommendCell" bundle:nil] forCellReuseIdentifier:@"cell"];
    [self setupScrollView];
    [self setupHotCollectionView];
    [self setupHeaderView];
//注册collectionView的cell ,和headerView
    [self.hotView registerClass:[HXHotCollectionCell class] forCellWithReuseIdentifier:hotID];
    [self.hotView registerNib:[UINib nibWithNibName:@"HXHotReusableView" bundle:nil] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:hotHeaderID];
    [self addClickHotCellNotice];
}

//设置背景颜色
- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [self setupNavigationItem];

    self.tableView.backgroundColor = HXGlobeColor ;
}
#pragma mark - 顶部活动view -
-(void)setupScrollView{
    XMGInfiniteScrollView *scrollView = [[XMGInfiniteScrollView alloc]initWithFrame:CGRectMake(0, -250 - HXMargin, SCREEN_WIDTH, 150)];
    NSArray *imageArray = @[[UIImage imageNamed:@"1"] , [UIImage imageNamed:@"2"] ,[UIImage imageNamed:@"3"]];
    scrollView.images = imageArray ;
    [self.tableView addSubview:scrollView];
}

-(void)setupActiveView{
    
    HXActiveView *activeView = [[HXActiveView alloc]init];
    activeView.frame = CGRectMake(0, -110 , SCREEN_WIDTH, 100);
    activeView.backgroundColor =[UIColor grayColor];
    [self.tableView addSubview:activeView];
}

-(void)setupHeaderView{
    
    UIView *headerSellView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 300)];
    headerSellView.backgroundColor =  HXGlobeColor ;
    CGFloat verticalBtnW = (SCREEN_WIDTH - 2*MinMargin) / 3 ;
    CGFloat verticalBtnH = 140 ;
    for (int i = 0; i< 3; i++) {
        UIButton *buttonV = [UIButton buttonWithType:(UIButtonTypeCustom)];
        buttonV.tag = i ;
        buttonV.frame = CGRectMake(i * (verticalBtnW + MinMargin), 0, verticalBtnW, verticalBtnH);
        [buttonV addTarget:self action:@selector(sellButtonClick:) forControlEvents:(UIControlEventTouchUpInside)];
        [buttonV setBackgroundImage:[UIImage imageNamed:@"2"] forState:(UIControlStateNormal)];
        [headerSellView addSubview:buttonV];
    }
    
    HXButtonView *view = [HXButtonView buttonView];
    view.frame = CGRectMake(0, 150, SCREEN_WIDTH, 140);
    [headerSellView addSubview:view];
    self.tableView.tableHeaderView = headerSellView ;
}

-(void)sellButtonClick : (UIButton *)button{
    NSLog(@"第%zd个头部按钮被点击",button.tag);
}

#pragma mark - tableview的代理及数据源方法 -

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 4 ;
}
-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    HXrecommendCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
        return cell ;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return  300;
}

#pragma mark - 设置导航条信息 -
#pragma mark - 导航按钮点击
-(void)leftButtonClick{

    HXQRCodeVC *qrCode = [[HXQRCodeVC alloc]init];
    [self.navigationController pushViewController:qrCode animated:YES];

}

-(void)rightButtonClick{
    
    HXSerachVC *search = [[HXSerachVC alloc]init];
    [self.navigationController pushViewController:search animated:YES];
    
}


#pragma mark - 热卖collectionView的搭建 -

-(void)setupHotCollectionView{
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc]init];
    layout.itemSize = CGSizeMake((SCREEN_WIDTH - 30) * 0.5, 300);
    layout.minimumInteritemSpacing = 10 ;
    layout.minimumLineSpacing = 10 ;
    UICollectionView *collectionView = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 3200) collectionViewLayout:layout];
    collectionView.showsHorizontalScrollIndicator = NO ;
    collectionView.backgroundColor = HXGlobeColor ;
    collectionView.dataSource = self;
    collectionView.delegate = self ;
    collectionView.scrollEnabled = NO ;
    self.hotView = collectionView ;
    collectionView.contentInset = UIEdgeInsetsMake(0, 10, 0, 10);
    self.tableView.tableFooterView = collectionView ;
}

#pragma mark - collectionViewheader -

-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return 20 ;
}

-(UICollectionReusableView*)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath{
    HXHotReusableView *header = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:hotHeaderID forIndexPath:indexPath];
    header.HotheaderLabel.text = @"鲜峰热卖";
    return header ;
}

-(UICollectionViewCell*)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    HXHotCollectionCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:hotID forIndexPath:indexPath];
    return cell ;
}

-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section
{
    return CGSizeMake(SCREEN_WIDTH, 30);
}

//接受通知
-(void)addClickHotCellNotice{
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(clickHotCellPushVC:) name:@"clickHotCell" object:nil];
}

-(void)clickHotCellPushVC :(NSNotification*)note{
//    HXHotCollectionCell *cell = note.object ;

#pragma mark - 建立模型,将cell的模型传递给expendVC

    UIStoryboard *storyBoard= [UIStoryboard storyboardWithName:@"HXExpandViewController" bundle:nil];
    HXExpandViewController *expandVC = [storyBoard instantiateInitialViewController];
    
    [self.navigationController pushViewController:expandVC animated:YES];
}


-(void)setupNavigationItem{
    
    HXButton *leftButton = [HXButton buttonWithTitle:@"扫一扫" NormalImage:@"icon_black_scancode" HighlightedImage:@"scanicon" andFontSize:10];
    
    [leftButton addTarget:self action:@selector(leftButtonClick) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem *leftBarItem = [[UIBarButtonItem alloc]initWithCustomView:leftButton];
    self.navigationItem.leftBarButtonItem = leftBarItem;
    
    HXButton *rightButton = [HXButton buttonWithTitle:@"搜索" NormalImage:@"icon_search" HighlightedImage:nil andFontSize:10];
    
    [rightButton addTarget:self action:@selector(rightButtonClick) forControlEvents:UIControlEventTouchUpInside];
    
    UIBarButtonItem *rightBarItem = [[UIBarButtonItem alloc]initWithCustomView:rightButton];
    
    self.navigationItem.rightBarButtonItem = rightBarItem ;
    
    
    [self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"v2_my_avatar_bg"] forBarMetrics:(UIBarMetricsDefault)];
    
    HXTopTitleView *titleView = [HXTopTitleView topTitleView];
    titleView.frame = CGRectMake(0, 0, 120, 30);
    
    self.navigationItem.titleView = titleView;
    
}


#pragma mark - 待解决问题 -
/*
1.collectionView与tableViewcell的分割线(已解决)
2.tableView的footerView高度计算(根据请求数据决定)
3.数据的赋值
4.下拉刷新界面()
5.导航条(封装代码)
6.整个应用内多处使用到类似首页界面的布局(tableView的cell加上collectionView),应该进行封装(抽取datasouce).
7.可以尝试创建collectionView对象,减少控制器代码
*/

@end
